<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/25
 * Time: 9:50
 */
namespace app\share\controller;

use app\common\model\IcmpPing;
use app\share\model\PingRecord;
use think\Controller;
use think\Validate;

class PingIp extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->ping_ip = new PingRecord();
        $this->icmp = new IcmpPing();
    }
    public function pingTest($ip)
    {
        $this->icmp->PingIp($ip);
    }
    public function pingAddress($address)
    {
        $this->icmp->PingIp($address);
    }
    //window ping每月定时任务
    public function pingMonth()
    {
        $this->ping_ip->downMore();
    }
    //window ping每天定时定时任务
    public function pingDay()
    {
        $this->ping_ip->timeAddPing();
    }
    //window ping每分钟定时任务
    public function pingIcMp()
    {
        $this->ping_ip->pingRule();
    }
    public function timePingList()
    {
        $input = input();
        if(isset($input['company_id']))
        {
            $where['company_id'] = $input['company_id'];
        }
        if(isset($input['depart_id']))
        {
            $where['depart_id'] = $input['depart_id'];
        }
        $where['ping_status'] = 2;
        $time  = time();
        $table = "ping_record_".$this->ping_ip->tableName($this->ping_ip->timeStart($time));
        $where['ping_sort'] = array( 'egt',($this->ping_ip->timeStart($time))*1000 );
        return $this->ping_ip->timePingList($table,$where);
    }
    //v2-13单位和部门下所有ip
    public function allIp()
    {
        $input = input();
        $validate = new Validate([
            ['company_id', 'require', '单位id不能为空'],
            ['depart_id', 'require', '部门id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        unset($input['token']);
        $this->ping_ip->allIp($input);
    }
    //v2-12所有单位
    public function allCompany()
    {
        $input = input();
        $validate = new Validate([
            ['company_id', 'require', 'id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        $this->ping_ip->allCompany($input['company_id']);
    }
    //v2-11下载更多接口
    public function moreDown()
    {
        $input = input();
        $page = empty($input['page']) ? 1 : $input['page'];
        $size = empty($input['size']) ? 10: $input['size'];
        return  $this->ping_ip->moreDown($page,$size);
    }
    //下载测试
    public function downMore()
    {
        return $this->ping_ip->downMore();
    }
    public function onlyIpDetail()
    {
        $input = input();
        $page = empty($input['page']) ? 1 : $input['page'];
        $size = empty($input['size']) ? 10: $input['size'];
        $validate = new Validate([
            ['ip', 'require', 'ip不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return  $this->ping_ip->onlyIpDetail(trim($input['ip']),$page,$size);
    }
    public function SouIp()
    {
        $input = input();
        $validate = new Validate([
            ['ip', 'require', 'id不能为空'],
            ['ping_sort', 'require', '批次不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return $this->ping_ip->SouIp($input['ip'],$input['ping_sort']);
    }
    //v2-10根据单位id联动查询部门
    public function companyDepart()
    {
        $input = input();
        $validate = new Validate([
            ['company_id', 'require', '单位ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return $this->ping_ip->companyDepart($input['company_id']);
    }


    //添加部门中单位显示
    public function listPart()
    {
        return $this->ping_ip->listPart();
    }
    //显示单位
    public function listCompany()
    {
        $input = input();
        $page = empty($input['page']) ? 1 : $input['page'];
        $size = empty($input['size']) ? 10: $input['size'];
        return $this->ping_ip->listCompany($page,$size);
    }
    //删除单位
    public function deleteCompany()
    {
        $input = input();
        $validate = new Validate([
            ['company_id', 'require', 'ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return $this->ping_ip->deleteCompany($input['company_id']);

    }
    //编辑单位
    public function editCompany()
    {
        $input = input();
        $validate = new Validate([
            ['company_id', 'require', 'ID不能为空'],
            ['p_id', 'require', '上级id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        $id = $input['company_id'];
        $p_id = $input['p_id'];
        unset($input['token']);
        unset($input['company_id']);
        unset($input['p_id']);
        $input['update_time'] = time();
        return $this->ping_ip->editCompany($id,$p_id,$input);
    }
    //管理后台添加单位
    public function addCompany()
    {
        $input = input();
        $validate = new Validate([
            ['company_name', 'require', '单位名称不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        unset($input['token']);
        $input['add_time'] = time();
        return $this->ping_ip->addCompany($input);

    }

    //手动添加定时执行
    public function timeAddPing()
    {
        $input = input();
        $validate = new Validate([
            ['day', 'require', '日期不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return $this->ping_ip->manualTimeAddPing($input['day']);
    }

    //26历史数据
    public function historyDownLoad()
    {
        ini_set('max_execution_time','0');
        ini_set('memory_limit','300M');
        $input = input();
        $page = empty($input['page']) ? 1 : $input['page'];
        $size = empty($input['size']) ? 10: $input['size'];
        $type = empty($input['type']) ? 1 : $input['type'];
        //时间条件
        $where = [];
        if($type == 1 || $type == 2)
        {}else{
            returnJson(-2001,  "参数错误")->send();die;
        }
        if( isset( $input['yestoday'] ) && isset( $input['time_sou'] ) )
        {
            returnJson(-2001,  "时间参数冲突")->send();die;
        }
        else if( isset( $input['yestoday'] ) )
        {
            //当前传1，表示今天的时间
            if( $input['yestoday'] == 1 )
            {
                $time  = time();
                $table = "ping_record_".$this->ping_ip->tableName($this->ping_ip->timeStart($time));
                $where['ping_sort'] = array( 'egt',($this->ping_ip->timeStart($time))*1000 );
            }else if( $input['yestoday'] == 2 )
            {
                $time = strtotime("-1 day");
                $table = "ping_record_".$this->ping_ip->tableName($this->ping_ip->timeStart($time));
                $start_time = ($this->ping_ip->timeStart($time))*1000;
                $end_time   = ($this->ping_ip->timeEnd($time))*1000;
                $where['ping_sort'] = array( array( 'egt', $start_time ), array( 'lt', $end_time ) );
            }else
            {
                returnJson(-2001,  "参数错误")->send();die;
            }
        }
        else if( isset( $input['time_sou'] ) )
        {
            if($input['time_sou'])
            {
                $table = "ping_record_".$this->ping_ip->tableName($this->ping_ip->timeStart($input['time_sou']));
                $start_time = ($this->ping_ip->timeStart($input['time_sou']))*1000;
                $end_time   = ($this->ping_ip->timeEnd($input['time_sou']))*1000;
                $where['ping_sort'] = array( array( 'egt', $start_time ), array( 'lt', $end_time ) );
            }else
            {
                returnJson(-2001,  "参数错误")->send();die;
            }
        }
        else
        {
            $time  = time();
            $table = "ping_record_".$this->ping_ip->tableName($this->ping_ip->timeStart($time));
            $where['ping_sort'] = array( 'egt',($this->ping_ip->timeStart($time))*1000 );
        }

        if(!isset($input['ping_status']))
        {
            $where['ping_status'] = 2;
        }else
        {
            if($input['ping_status'] === "")
            {}else
            {
                if($input['ping_status']==1)
                {
                    $where['ping_status'] = 1;
                }else if($input['ping_status']==2)
                {
                    $where['ping_status'] = 2;
                }else
                {
                    returnJson(-2001,  "参数错误")->send();die;
                }
            }
        }
        if( isset($input['net_name']) )
        {
            //ip格式判断
            if( filter_var( $input['net_name'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ){        //IPv6
                $where['ip_address'] = trim($input['net_name']);
            }elseif( filter_var( $input['net_name'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ){    //IPv4
                $where['ip_address'] = trim($input['net_name']);
            }else{
                $where['net_name'] = ['like',"%".trim($input['net_name'])."%"];
            }
        }



        return $this->ping_ip->historyDownLoad( $table, $where, $page, $size, $type);
    }
    public function onlyDetail()
    {
        $input = input();
        $validate = new Validate([
            ['depart_id', 'require', '部门id不能为空'],
//            ['ping_status', 'require|between:1,2', '状态不能为空|状态码错误'],
            ['ping_sort', 'require', '批次不能为空'],
//            ['server_id', 'require', '服务商id不能为空'],
            ['company_id', 'require', '单位id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return $this->ping_ip->onlyDetail($input);
    }

    //25.ping滚动条
    public function timeShow()
    {
        $input = input();
        $page = empty($input['page']) ? 1 : $input['page'];
        $size = empty($input['size']) ? 10: $input['size'];
        return $this->ping_ip->timeShow($page,$size);
    }

    //24实现网络展示
    public function timeDetail()
    {
        return $this->ping_ip->timeDetail();
    }
    //29.工作台数据
    public function workDetail()
    {
        return $this->ping_ip->workDetail();
    }
    //23.网络测试规则列表数据删除
    public function setDel()
    {
        $input = input();
        $validate = new Validate([
            ['set_id', 'require', 'id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return $this->ping_ip->setDel($input['set_id']);
    }
    //22.网络测试规则列表修改状态
    public function setStatus()
    {
        $input = input();
        $validate = new Validate([
            ['set_status', 'require|between:1,2', '启用状态不能为空|状态值错误'],
            ['set_id', 'require', 'id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        return $this->ping_ip->setDisplay($input['set_id'],$input['set_status']);
    }
    //21.网络测试规则列表显示
    public function setList()
    {
        $input = input();
        $page = empty($input['page']) ? 1 : $input['page'];
        $size = empty($input['size']) ? 10: $input['size'];
        return $this->ping_ip->setShow($page,$size);
    }
    //ping ip地址库所有ip，网络状态存库
    public function pingAllIp()
    {
        return $this->ping_ip->pingRule();
    }
    public function updateSet()
    {
        $input = input();
        $validate = new Validate([
            ['set_id', 'require', '规则ID不能为空'],
            ['set_day', 'require', '规则天不能为空'],
            ['set_start_time', 'require', '开始时间不能为空'],
            ['set_end_time', 'require', '结束时间不能为空'],
            ['set_holiday_status', 'require|between:1,2', '节假日设置不能为空|节假日设置格式错误'],
            ['interval_time', 'require|/^[1-9][0-9]*$/|between:10,1440', '间隔时间不能为空|间隔时间必须为正整数|间隔时间应该在10分钟到24小时之间'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        if( strtotime( $input['set_start_time'] ) > strtotime( $input['set_end_time'] ) )
        {
            returnJson(-1002,  "结束时间不能小于开始时间")->send();die;
        }
        $where['set_status'] = 1;
        $where['set_id']     = $input['set_id'];
        if($this->ping_ip->setOnline($where))
        {
            returnJson(-1009,  "请先停止该定时任务后再进行编辑")->send();die;
        }
        $set['set_day']             = trim($input['set_day']);
        $set['set_start_time']      = trim($input['set_start_time']);
        $set['set_end_time']        = trim($input['set_end_time']);
        $set['set_holiday_status']  = trim($input['set_holiday_status']);
        $set['interval_time']       = trim($input['interval_time']);
        $set['set_time']            = time();
        $set['set_user_id']         = get_user_id($input['token']);
        return $this->ping_ip->setUpdateDate(trim($input['set_id']), $set);
    }
    //网络测试规则设置添加
    public function setAdd()
    {
        $input = input();
        $validate = new Validate([
            ['set_day', 'require', '规则天不能为空'],
            ['set_start_time', 'require', '开始时间不能为空'],
            ['set_end_time', 'require', '结束时间不能为空'],
            ['set_holiday_status', 'require|between:1,2', '节假日设置不能为空|节假日设置格式错误'],
            ['set_status', 'require|between:1,2', '启用状态不能为空|启用状态设置格式错误'],
            ['interval_time', 'require|/^[1-9][0-9]*$/|between:10,1440', '间隔时间不能为空|间隔时间必须为正整数|间隔时间应该在10分钟到24小时之间'],
        ]);
        if(!$validate->check($input))
        {
            return ['code'=>-1002,'msg'=>$validate->getError()];
        }
        if( strtotime( $input['set_start_time'] ) > strtotime( $input['set_end_time'] ) )
        {
            returnJson(-1002,  "结束时间不能小于开始时间")->send();die;
        }
        if($input['set_status'] == 1){
            $where['set_status'] = 1;
            return $this->ping_ip->setStatus($where);
        }
        $set['set_day']             = trim($input['set_day']);
        $set['set_start_time']      = trim($input['set_start_time']);
        $set['set_end_time']        = trim($input['set_end_time']);
        $set['set_holiday_status']  = trim($input['set_holiday_status']);
        $set['set_status']          = trim($input['set_status']);
        $set['interval_time']       = trim($input['interval_time']);
        $set['set_time']            = time();
        $set['set_user_id']         = get_user_id($input['token']);
        return $this->ping_ip->setAddDate($set);
    }



}